package com.itljm.cxrMarket.common;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 * @author ljm
 * @date 2024/11/1 17:00
 */
@Data
@NoArgsConstructor//无参构造函数
@AllArgsConstructor //满参的构造函数
public class Result<T> implements Serializable {
    //是否成功
    private boolean flag = true;
    // 返回类型状态码
    private Integer code;
    // 返回消息
    private String message;
    // 返回的业务数据
    private T data;

    public Result(Integer code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public Result(Integer ok, String okMsg, Map<String, Object> map) {
        this.code = ok;
        this.message = okMsg;
        this.data = (T) map;
    }

    public Result(Integer code, String msg) {
        this.code = code;
        this.message = msg;
    }

    public Result(boolean flag, Integer code, String msg) {
        this.flag = flag;
        this.code = code;
        this.message = msg;
    }

    public static <T> Result<T> ok(String msg) {
        return new Result<T>(StatusCode.SUCCESS.getCode(), msg);
    }

    public static <T> Result<T> ok(StatusCode statusCode) {
        return new Result<T>(statusCode.getCode(), statusCode.getMessage());
    }

    public static <T> Result<T> ok(T data) {
        return new Result<T>(true, StatusCode.SUCCESS.getCode(),StatusCode.SUCCESS.getMessage(),data);
    }

    public static <T> Result<T> ok(String msg, T data) {
        return new Result<T>(true, StatusCode.SUCCESS.getCode(),msg,data);
    }

    public static <T> Result<T> ok() {
        return new Result<T>(StatusCode.SUCCESS.getCode(), StatusCode.SUCCESS.getMessage());
    }

    public static <T> Result<T> fail(String msg) {
        return new Result<T>(false, StatusCode.BAD_REQUEST.getCode(),msg);
    }

    public static <T> Result<T> fail(Integer code, String msg) {
        return new Result<T>(false, code, msg);
    }
    public static <T> Result<T> fail(StatusCode statusCode) {
        return new Result<T>(false, statusCode.getCode(), statusCode.getMessage());
    }

    public static <T> Result<T> fail(String msg, T data) {
        return new Result<T>(false, StatusCode.BAD_REQUEST.getCode(),msg,data);
    }

    public static <T> Result<T> fail() {
        return new Result<T>(false, StatusCode.BAD_REQUEST.getCode(), StatusCode.FAILURE.getMessage());
    }
}
